/*
// =====================================================================================
// 
//       Filename:  gzlib_util.cpp
// 
//    Description:  elwin library utility tools
// 
//        Version:  1.0
//        Created:  07/04/2013 11:45:47 AM
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  Elwin.Gao (elwin), elwin.gao4444@gmail.com
//        Company:  
// 
// =====================================================================================
*/

#include "gzlib_util.hpp"

// =====================================================================================
//  Description:  常用功能
// =====================================================================================

long send_all(int socket, const char *buf, size_t size, int flags)
{
	long ret = 0;
	long finished = 0;

	assert(size < LONG_MAX);
	do {
		ret = send(socket, buf+finished, size-finished, flags);
		if (ret >= 0) {
			finished += ret;
		} else {
			return -1; 
		}
	} while(ret && finished < (long)size);

	return finished;
}		// -----  end of function send_all  -----

long recv_all(int socket, char *buf, size_t size, int flags)
{
	long ret = 0;
	long finished = 0;

	assert(size < LONG_MAX);
	do {
		ret = recv(socket, buf+finished, size-finished, flags);
		if (ret >= 0) {
			finished += ret;
		} else {
			return -1; 
		}
	} while(ret && finished < (long)size);

	return finished;
}		// -----  end of function recv_all  -----

